import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;


class Student {
    //私有属性name
    private String name = "bit";
    //公有属性age
    public int age = 18;
    //不带参数的构造方法
    public Student(){
        System.out.println("Student()");
    }
    private Student(String name,int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String,name)");
    }
    private void eat(){
        System.out.println("i am eat");
    }
    public void sleep(){
        System.out.println("i am pig");
    }
    private void function(String str) {
        System.out.println(str);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void testRefect1(){
        //调用其他构造方法
        try {
            //创建类对象
            Class<?>c1=Class.forName("Student");
            //调用其他构造方法
            Constructor<?>constructor =c1.getDeclaredConstructor(String.class,int.class);
            //确认访问私有方法
            constructor.setAccessible(true);
            //调用实例化对象
            Student student=(Student) constructor.newInstance("wzj",19);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static void testRefect2() {
        //反射属性
        try {
            //创建类对象
            Class<?>c1=Class.forName("Student");
            //c1创建一个student对象
            Student stu=(Student) c1.newInstance();
            //调用反射属性的方法，并且接受Field实例
            Field field=c1.getDeclaredField("name");
            //用field去修改某个对象的name变量的值
            field.set(stu,"wzj");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void testRefect3() {
        try {
            //得到Student的类对象
            Class<?>c1=Class.forName("Student");
            //创建学生对象
            Student stu=(Student) c1.newInstance();
            //调用反射方法，返回Method的实例
            Method method=c1.getDeclaredMethod("function", String.class);
            //允许访问私有方法
            method.setAccessible(true);
            //给这个方法传参
            method.invoke(stu,"我是给你传参的");


        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws ClassNotFoundException {
        testRefect3();
    }
}
